#include"StdAfx.h"
#include"StaticQuad.h"

StaticQuad::StaticQuad(  XMFLOAT3 cenPos, float width, float height, LPCSTR texFile )
	: SceneObject( cenPos, width, height ), m_SRV( 0 ), m_texFile( texFile )
{
}

StaticQuad::~StaticQuad( )
{
	ReleaseCOM( m_SRV );
}

bool StaticQuad::Init( ID3D11Device* device )
{
	bool result;

	result = SceneObject::Init( device );
	if( !result )
		return false;

	result = CreateTexture( device );
	if( !result )
		return false;

	return true;
}

void StaticQuad::DrawObject( ID3D11DeviceContext* d3dContext, CXMMATRIX view, CXMMATRIX proj )
{
	EffectMgr::m_TexFX->SetWorld( GetWorld( ) );
	EffectMgr::m_TexFX->SetView( view );
	EffectMgr::m_TexFX->SetProj( proj );
	EffectMgr::m_TexFX->SetTexture( m_SRV );

	unsigned int stride = sizeof( Geometry::BasicVertex );
    unsigned int offset = 0;

	d3dContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
	d3dContext->IASetVertexBuffers( 0, 1, &m_VB, &stride, &offset );
	d3dContext->IASetIndexBuffer( m_IB, DXGI_FORMAT_R32_UINT, 0 );
	d3dContext->IASetInputLayout( m_inputLayout );

	D3DX11_TECHNIQUE_DESC techDesc;
	EffectMgr::m_TexFX->m_Tech->GetDesc( &techDesc );
	EffectMgr::m_TexFX->m_Tech->GetPassByIndex( 0 )->Apply( 0, d3dContext );

	d3dContext->DrawIndexed( m_indexCount, 0, 0 );
}

bool StaticQuad::CreateTexture( ID3D11Device* device )
{
	HRESULT hr;
	
	hr = D3DX11CreateShaderResourceViewFromFile( device, m_texFile, 0, 0, &m_SRV, 0 ); 
	if( FAILED( hr ) )
	{
		DXTRACE_MSG( "Failed to create SRV!" );
		return false;
	}
	
	return true;
}